<?php require_once(BASEPATH.'application/controllers/validaciones.php');
class Sprv extends validaciones {

	function sprv(){
		parent::Controller();
		$this->load->library('rapyd');
	}

	function index(){
			redirect('compras/sprv/filteredgrid');
	}

	function filteredgrid(){

		$this->rapyd->load('datafilter','datagrid');
		$this->rapyd->uri->keep_persistence();

		$filter = new DataFilter('B&uacute;squeda de Proveedores', 'sprv');

		$filter->proveed = new inputField('C&oacute;digo','proveed');
		$filter->proveed->size=13;

		$filter->nombre = new inputField('Nombre', 'nombre');
		$filter->nombre->maxlength=30;
		
		$filter->rif = new inputField('RUC', 'rif');
		$filter->rif->size=18;
		$filter->rif->maxlength=30;

		$filter->tipo = new dropdownField('Tipo', 'tipo');
		$filter->tipo->option('','Todos');
		$filter->tipo->options(array('1'=> 'Jur&iacute;dico Domiciliado','2'=>'Residente', '3'=>'Jur&iacute;dico No Domiciliado','4'=>'No Residente','5'=>'Excluido del Libro de Compras','0'=>'Inactivo'));
		$filter->tipo->style = 'width:200px';
		
		$filter->grupo = new dropdownField("Grupo", "grupo");
		$filter->grupo->option("","Todos");
		$filter->grupo->options("SELECT grupo,gr_desc,grupo FROM grpr ORDER BY gr_desc");
		$filter->grupo->style = "width:220px";

		$filter->buttons('reset','search');
		$filter->build("dataformfiltro");
		
		function boton($tipo=''){
			if($tipo<>'0'){
				return '1';
			}else{
				return '2';
			}
		}

		$uri = anchor('compras/sprv/dataedit/show/<#proveed#>','<#proveed#>');

		$grid = new DataGrid('Lista de Proveedores');
		$grid->order_by('proveed','ASC');
		$grid->per_page = 10;
		$grid->use_function('boton');
			
		$uri_2  = anchor('compras/sprv/dataedit/show/<#proveed#>',img(array('src'=>'images/editar.png','border'=>'0','alt'=>'Editar','height'=>'16','title'=>'Editar')));
		$uri_2 .= img(array('src'=>'images/<boton><#tipo#></boton>.gif','border'=>'0','alt'=>'Estado','height'=>'12','title'=>'Estado'));
			
		$grid->column('Acci&oacute;n',$uri_2);
		$grid->column_orderby('C&oacute;digo',$uri,'codigo');
		$grid->column_orderby('Nombre','nombre','nombre');
		$grid->column_orderby('RUC','rif','rif');
		$grid->column_orderby('Telefono 1','telefono','telefono');
		$grid->column_orderby('Telefono 2','telefono2','telefono');
		$grid->column_orderby('Fax','fax','telefono');
		$grid->column_orderby('Ciudad','ciudad','telefono');
		$grid->column_orderby('Contacto','contacto','contacto');
		//$grid->column_orderby('% Ret.','reteiva','reteiva','align=\'right\'');
		
		$grid->add('compras/sprv/dataedit/create','Nuevo Proveedor');
		$grid->build('datagridST');


//************ SUPER TABLE ************* 
		$extras = '
<script type="text/javascript">
//<![CDATA[
(function() {
	var mySt = new superTable("demoTable", {
	cssSkin : "sSky",
	fixedCols : 1,
	headerRows : 1,
	onStart : function () {	this.start = new Date();},
	onFinish : function () {document.getElementById("testDiv").innerHTML += "Finished...<br>" + ((new Date()) - this.start) + "ms.<br>";}
	});
})();
//]]>
</script>
';
		$style ='
<style type="text/css">
.fakeContainer { /* The parent container */
    margin: 5px;
    padding: 0px;
    border: none;
    width: 900px; /* Required to set */
    height: 400px; /* Required to set */
    overflow: hidden; /* Required to set */
}
</style>	
';
//****************************************


		$data['content'] = $grid->output;
		$data['filtro']  = $filter->output;
		
		$data['title']   = '<h1>Proveedores</h1>';

		$data['style']   = $style;
		$data['style']  .= style('superTables.css');

		$data['extras']  = $extras;		

		$data['head']    = script('jquery.js');
		$data["head"]   .= script('superTables.js');
		$data['head']   .= $this->rapyd->get_head();
		$this->load->view('view_ventanas', $data);
	}

	function dataedit(){
		$this->rapyd->load('dataedit');

		$script ='
		$(function() {
			$(".inputnum").numeric(".");
		});

		}';

		$edit = new DataEdit(' ', 'sprv');
		$edit->script($script, 'create');
		$edit->script($script, 'modify');
		$edit->back_url = site_url('compras/sprv/filteredgrid');

		$edit->pre_process('delete','_pre_del');
		$edit->post_process('insert','_post_insert');
		$edit->post_process('update','_post_update');
		$edit->post_process('delete','_post_delete');

		//$lproveed='<a href="javascript:ultimo();" title="Consultar ultimo codigo ingresado" onclick="">Consultar &uacute;ltimo c&oacute;digo ingresado</a>';
		$edit->proveed  = new inputField('C&oacute;digo', 'proveed');
		$edit->proveed->rule = 'trim|required|callback_chexiste';
		$edit->proveed->mode = 'autohide';
		$edit->proveed->size = 13;
		$edit->proveed->maxlength =5;
		//$edit->proveed->append($lproveed);
		$edit->proveed->group = 'Identificaci&oacute;n';

		$edit->nombre = new inputField('Nombre Fiscal/Completo', 'nombre');
		$edit->nombre->rule = 'trim|strtoupper|required';
		$edit->nombre->size = 60;
		$edit->nombre->maxlength =100;
		$edit->nombre->group ="Identificaci&oacute;n";

		$lriffis='<a href="javascript:consulrif();" title="Consultar RIF en el SENIAT" onclick="" style="color:red;font-size:9px;border:none;">SENIAT</a>';
		$edit->rif =  new inputField('RUC', 'rif');
		$edit->rif->rule = "trim|strtoupper|required";
		//$edit->rif->append($lriffis);
		$edit->rif->maxlength=13;
		$edit->rif->size =20;
		$edit->rif->group = 'Identificaci&oacute;n';

		$edit->contacto = new inputField("Contacto", "contacto");
		$edit->contacto->size =41;
		$edit->contacto->rule ="trim";
		$edit->contacto->maxlength =40;
		$edit->contacto->group ="Identificaci&oacute;n";
		
		$edit->telefono = new inputField("Tel&eacute;fono 1", "telefono");
		$edit->telefono->size = 41;
		$edit->telefono->rule = "trim";
		$edit->telefono->group = "Identificaci&oacute;n";
		$edit->telefono->maxlength =40;
				
		$edit->telefono2 = new inputField("Tel&eacute;fono 2", "telefono2");
		$edit->telefono2->size = 41;
		$edit->telefono2->rule = "trim";
		$edit->telefono2->group = "Identificaci&oacute;n";
		$edit->telefono2->maxlength =40;
		
		$edit->fax = new inputField("Fax", "fax");
		$edit->fax->size = 41;
		$edit->fax->rule = "trim";
		$edit->fax->group = "Identificaci&oacute;n";
		$edit->fax->maxlength =40;
		
		$edit->direc1 = new textareaField("Direcci&oacute;n ",'direc');
		$edit->direc1->rule ="trim|strtoupper";
		$edit->direc1->cols = 60;
		$edit->direc1->rows =3;
		$edit->direc1->group = 'Identificaci&oacute;n';
		
		$edit->ciudad = new inputField("Ciudad",'ciudad');
		$edit->ciudad->rule ="trim|strtoupper";
		$edit->ciudad->size = 41;
		$edit->ciudad->group = 'Identificaci&oacute;n';
		
		$edit->apartado  = new inputField("Apartado Postal","apartado");
		$edit->apartado->size = 6;
		$edit->apartado->css_class='inputnum';	
		$edit->apartado->group = "Identificaci&oacute;n";

		$edit->grupo = new dropdownField("Grupo", "grupo");
		$edit->grupo->option("","Seleccionar");
		$edit->grupo->options("SELECT grupo,gr_desc,grupo FROM grpr ORDER BY gr_desc");
		$edit->grupo->style = "width:190px";
		//$edit->grupo->rule = "required";
		$edit->grupo->group = "Identificaci&oacute;n";

		$edit->tipo = new dropdownField("Tipo", "tipo");
		$edit->tipo->option("","Seleccionar");
		$edit->tipo->options(array("1"=> "Jur&iacute;dico Domiciliado","2"=>"Residente", "3"=>"Jur&iacute;dico No Domiciliado","4"=>"No Residente","5"=>"Excluido del Libro de Compras","0"=>"Inactivo"));
		$edit->tipo->style = "width:190px";
		$edit->tipo->rule = "required";
		$edit->tipo->group = "Identificaci&oacute;n";

		$edit->tiva  = new dropdownField("Origen", "tiva");
		$edit->tiva->option("N","Nacional");
		$edit->tiva->options(array("N"=>"Nacional","I"=>"Internacional","O"=>"Otros"));
		$edit->tiva->style='width:190px;';
		$edit->tiva->group = "Identificaci&oacute;n";
		
		$edit->reteiva  = new inputField("% de Retenci&oacute;n","reteiva");
		$edit->reteiva->size = 6;
		$edit->reteiva->css_class='inputnum';	
		$edit->reteiva->group = "Identificaci&oacute;n";

		$edit->email  = new inputField("Email", "email");
		$edit->email->rule = "trim|valid_email";
		$edit->email->size =30;
		$edit->email->maxlength =50;
		$edit->email->group = "Contacto";

		$edit->url   = new inputField("URL", "url");
		$edit->url->group = "Contacto";
		$edit->url->rule = "trim";
		$edit->url->size =30;
		$edit->url->maxlength =30;

		$edit->observa  = new inputField("Observaci&oacute;n", "observa");
		$edit->observa->group = "Contacto";
		$edit->observa->rule = "trim";
		$edit->observa->size = 41;

		$edit->buttons("modify", "save", "undo", "delete", "back");
		$edit->build();
		
		//$data['filtro']  = ' ';
		$data['title'] = heading('('.$edit->proveed->value.') '.$edit->nombre->value);
		$data["head"]    = script("jquery.js");
		$data["head"]   .= script("plugins/jquery.numeric.pack.js");
		$data["head"]   .= script("plugins/jquery.floatnumber.js");
		$data["head"]   .= $this->rapyd->get_head();
		
		$conten["form"]  =&  $edit;
		
		IF($edit->_status=='create'){	
			$data['content'] = $edit->output;
			$this->load->view('view_ventanas', $data);
		}else{
			$data['content'] = $this->load->view('view_sprv', $conten,true);
			$this->load->view('view_ventanas', $data);
		}
		
	}

	function _pre_del($do) {
		$codigo=$do->get('proveed');
		$chek =  $this->datasis->dameval("SELECT count(*) FROM sprm WHERE cod_prv='$codigo'");
		if ($chek > 0){
			$do->error_message_ar['pre_del'] = $do->error_message_ar['delete']='Proveedor con Movimiento no puede ser Eliminado';
			return False;
		}
		return True;
	}
	function _post_insert($do){
		$codigo=$do->get('proveed');
		$nombre=$do->get('nombre');
		logusu('sprv',"PROVEEDOR $codigo NOMBRE $nombre CREADO");
	}
	function _post_update($do){
		$codigo=$do->get('proveed');
		$nombre=$do->get('nombre');
		logusu('sprv',"PROVEEDOR $codigo NOMBRE $nombre MODIFICADO");
	}
	function _post_delete($do){
		$codigo=$do->get('proveed');
		$nombre=$do->get('nombre');
		logusu('sprv',"PROVEEDOR $codigo NOMBRE $nombre ELIMINADO");
	}
	function chexiste(){
		$codigo=$this->input->post('proveed');
		$rif=$this->input->post('rif');
		$chek=$this->datasis->dameval("SELECT COUNT(*) FROM sprv WHERE proveed='$codigo'");
		if ($chek > 0){
			$nombre=$this->datasis->dameval("SELECT nombre FROM sprv WHERE proveed='$codigo'");
			$this->validation->set_message('chexiste',"El codigo $codigo ya existe para el proveedor $nombre");
			return FALSE;
		}elseif(strlen($rif)>0){
			$chek=$this->datasis->dameval("SELECT COUNT(*) FROM sprv WHERE rif='$rif'");
			if ($chek > 0){
				$nombre=$this->datasis->dameval("SELECT nombre FROM sprv WHERE rif='$rif'");
				$this->validation->set_message('chexiste',"El rif $rif ya existe para el proveedor $nombre");
				return FALSE;
			}else {
				return TRUE;
			}
		}else{
			return TRUE;
		}
	}

	function _pre_insert($do){
		$rif=$do->get('rif');
		$chek=$this->datasis->dameval("SELECT COUNT(*) FROM sprv WHERE rif='$rif'");
		if($chek > 0){
			//$nombre=$this->datasis->dameval("SELECT nombre FROM sprv WHERE rif='$rif'");
			$do->error_message_ar['pre_insert'] = $do->error_message_ar['insert']='bobo';
			return FALSE;
		}else {
		return TRUE;
		}
	}

	function update(){
		$mSQL=$this->db->query('UPDATE sprv SET reteiva=75 WHERE reteiva<>100');
	}

	function uproveed(){
		$consulproveed=$this->datasis->dameval('SELECT MAX(proveed) FROM sprv');
		echo $consulproveed;
	}

	function consulta(){  
		$this->load->helper('openflash');
		$this->rapyd->load("datagrid");
		$fields = $this->db->field_data('sprv');
		$url_pk = $this->uri->segment_array();
		$coun=0; $pk=array();
		foreach ($fields as $field){
			if($field->primary_key==1){
				$coun++;
				$pk[]=$field->name;
			}
		}
		$values=array_slice($url_pk,-$coun);
		$claves=array_combine (array_reverse($pk) ,$values );

		$mCodigo = $this->datasis->dameval("SELECT proveed FROM sprv WHERE id=".$claves['id']."");
		
		$grid = new DataGrid('Ventas por Mes');
		$grid->db->_protect_identifiers=false;
		$grid->db->select( array('a.tipo_doc','a.fecha', 'a.numero', 'a.monto', 'a.abonos', 'a.monto-a.abonos saldo' ) );
		$grid->db->from('sprm a');
		$grid->db->where('a.cod_prv', $mCodigo );
		$grid->db->where('a.monto <> a.abonos');
		$grid->db->where('a.tipo_doc IN ("FC","ND","GI") ' );
		$grid->db->orderby('a.fecha');
			
		$grid->column("Fecha"   ,"fecha" );
		$grid->column("Tipo", "tipo_doc",'align="CENTER"');
		$grid->column("Numero",  "numero",'align="LEFT"');
		$grid->column("Monto",    "<nformat><#monto#></nformat>",  'align="RIGHT"');
		$grid->column("Abonos",  "<nformat><#abonos#></nformat>",'align="RIGHT"');
		$grid->column("Saldo",  "<nformat><#saldo#></nformat>",'align="RIGHT"');
		$grid->build();

		$nombre = $this->datasis->dameval("SELECT nombre FROM sprv WHERE id=".$claves['id']." ");

		$data['content'] = $grid->output;
		$data["head"]     = script("plugins/jquery.numeric.pack.js").script("plugins/jquery.floatnumber.js").$this->rapyd->get_head();
		$data['title']    = '<h1>Consulta de Proveedor</h1>';
		$data["subtitle"] = "
			<div align='center' style='border: 2px outset #EFEFEF;background: #EFEFEF;font-size:18px'>
				<a href='javascript:javascript:history.go(-1)'>(".$mCodigo.") ".$nombre."</a>
			</div>";
		$this->load->view('view_ventanas', $data);
		
	}



	function instalar(){

		$mSQL='ALTER TABLE `sprv` DROP PRIMARY KEY';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD id INT AUTO_INCREMENT PRIMARY KEY';
		$this->db->simple_query($mSQL);
		//$mSQL='ALTER TABLE `sprv` ADD UNIQUE `id` (`id`)';
		//$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD PRIMARY KEY `id` (`id`)';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `copre` VARCHAR(11) DEFAULT NULL NULL AFTER `cuenta` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `ocompra` CHAR(1) DEFAULT NULL NULL AFTER `copre` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `dcredito` DECIMAL(3,0) DEFAULT "0" NULL AFTER `ocompra` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `despacho` DECIMAL(3,0) DEFAULT NULL NULL AFTER `dcredito` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `visita` VARCHAR(9) DEFAULT NULL NULL AFTER `despacho` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `cate` VARCHAR(20) NULL AFTER `visita` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `reteiva` DECIMAL(7,2) DEFAULT "0.00" NULL AFTER `cate` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` ADD `ncorto` VARCHAR(20) DEFAULT NULL NULL AFTER `nombre` ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` CHANGE `direc1` `direc1` VARCHAR(105) DEFAULT NULL NULL  ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` CHANGE `direc2` `direc2` VARCHAR(105) DEFAULT NULL NULL  ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` CHANGE `direc3` `direc3` VARCHAR(105) DEFAULT NULL NULL  ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` CHANGE `nombre` `nombre` VARCHAR(60) DEFAULT NULL NULL  ';
		$this->db->simple_query($mSQL);
		$mSQL='ALTER TABLE `sprv` CHANGE `nomfis` `nomfis` VARCHAR(200) DEFAULT NULL NULL  ';
		$this->db->simple_query($mSQL);
	}
}
?>
